package cn.uyunsky.platform.service;

import cn.uyunsky.platform.config.ConfigFactory;
import cn.uyunsky.platform.config.reader.SqlTemplateReader;
import cn.uyunsky.platform.util.sql.SqlPlan;
import cn.uyunsky.platform.util.sql.SqlPlanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.IncorrectResultSizeDataAccessException;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * <p>Description: 基础服务基类,无其他引用;一半用此基类配合hibernate template使用</p>
 */
public abstract class IServiceAbstract implements IService {

	public Logger logger = LoggerFactory.getLogger(this.getClass());

	/*配置工厂*/
	private ConfigFactory configFactory;

	public void setConfigFactory(ConfigFactory configFactory) {
		this.configFactory = configFactory;
	}

	public <T> T getReader(Class<T> readerClass) {
		return configFactory.getReader(readerClass);
	}

    public String getSql(String sqlid) {
        SqlTemplateReader reader = getReader(SqlTemplateReader.class);
        return reader.getSql(sqlid);
    }


	/**
	 * <p>Description: 从命名占位符sql中获取对应问号占位符参数序列</p>
	 * 参数说明:
	 * @param sqlplan
	 * @param args
	 * @return 
	 */
	public Object[] getArgs(SqlPlan sqlplan, Map args) {
		List namedList = sqlplan.getNameList();
		Object[] argsTemp = new Object[namedList.size()];//参数
		for (int i = 0; i < namedList.size(); i++) {
			String named = (String) namedList.get(i);
			argsTemp[i] = args.get(named);
		}
		return argsTemp;
	}
}
